package cn.edu.scau.cmi.wangmaoren.client.classicsDesignPattern;

import java.util.ArrayList;
import java.util.List;
import af.javafx.layout.AfLinearLayout;
import af.javafx.scrollpane.AfListPane;
import cn.edu.scau.cmi.wangmaoren.adapter.AdapterOfClass;
import cn.edu.scau.cmi.wangmaoren.adapter.AdapterOfObject;
import cn.edu.scau.cmi.wangmaoren.adapter.Student;
import cn.edu.scau.cmi.wangmaoren.adapter.Target;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class AdapterClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();
	private Button classAdapterButton = new Button("类适配器添加");
	private Button objectAdapterButton = new Button("对象适配器添加");
	private Label blankLeft = new Label();
	private Label blankRight = new Label();
	private TextField idText = new TextField();
	private TextField nameText = new TextField();
	private AfListPane listPane = new AfListPane();

	private List<Student> studentList = new ArrayList<Student>();//学生队列
	private Target classTarget = new AdapterOfClass();//类适配器
	private Target objectTarget = new AdapterOfObject();//对象适配器

	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();// 初始化界面
			
			Scene scene = new Scene(root, 900, 463);
			scene.getStylesheets().add( getClass().getResource("/cn/edu/scau/cmi/wangmaoren/adapter/application.css").toExternalForm());
			listPane.getStyleClass().add("student-list");
			primaryStage.setTitle("适配器模式");
			primaryStage.setScene(scene);
			primaryStage.show();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}
	
	public void openWindow()
	{
		initLayout();//初始化界面
		
		Scene scene = new Scene(root,900, 463);
		Stage stage = new Stage();
		stage.setTitle("适配器模式");
		stage.setScene(scene);
		stage.show();
	}

	/*
	 * 添加学生到队列，使用类适配器方式实现排序功能
	 */
	private void classAdapterAddStudent()
	{
		String id = idText.getText();
		String name = nameText.getText();

		if (!id.isEmpty() && !name.isEmpty())
		{
			idText.clear();
			nameText.clear();

			studentList.add(new Student(Integer.valueOf(id), name));
			studentList = classTarget.doTarget(studentList);//使用类适配器方式实现排序功能
			loadList(studentList);
		}
	}

	/*
	 * 添加学生到队列，使用对象适配器方式实现排序功能
	 */
	private void objectAdapterAddStudent()
	{
		String id = idText.getText();
		String name = nameText.getText();

		if (!id.isEmpty() && !name.isEmpty())
		{
			idText.clear();
			nameText.clear();

			studentList.add(new Student(Integer.valueOf(id), name));
			studentList = objectTarget.doTarget(studentList);//使用对象适配器方式实现排序功能
			loadList(studentList);
		}
	}

	/*
	 * 加载学生信息列表
	 */
	public void loadList(List<Student> studentList)
	{
		listPane.getItems().clear();// 清空当前列表信息
		for (Student student : studentList)
		{
			addRow(student);// 添加学生信息到队列中
		}
	}

	/*
	 * 添加学生信息到队列中
	 */
	public void addRow(Student data)
	{
		ItemView view = new ItemView(data); // 创建一个View
		view.setUserData(data); // 关联一个数据对象到View
		listPane.add(view); // 将View添加到 ListPane
	}

	/*
	 * 列表每一行的显示
	 */
	private class ItemView extends AfLinearLayout
	{
		Label idLabel = new Label();
		Label nameLabel = new Label();
		Label blankLabel = new Label("");

		public ItemView(Student data)
		{
			this.orientation(Orientation.HORIZONTAL).spacing(4);
			this.getChildren().addAll(blankLabel, idLabel, nameLabel);
			this.layout("2,2,2");
			this.getStyleClass().add("item");

			idLabel.setText(String.valueOf(data.getId()));
			nameLabel.setText(data.getName());
		}
	}

	/*
	 * 初始化界面
	 */
	private void initLayout()
	{
		root.orientation(Orientation.VERTICAL).spacing(20).padding(20);
		line.spacing(20);

		line.getChildren().addAll(blankLeft, idText, nameText, classAdapterButton, objectAdapterButton, blankRight);
		line.layout("1 180px 180px 200px 200px 1");
		root.add(line, "50px");
		root.add(listPane, "1");

		idText.setPromptText("请输入学生学号");
		nameText.setPromptText("请输入学生姓名");
		
		classAdapterButton.setFont(Font.font("华文中宋", 20));
		objectAdapterButton.setFont(Font.font("华文中宋", 20));
		idText.setFont(Font.font("华文中宋", 19));
		nameText.setFont(Font.font("华文中宋", 19));

		Image classAdapterImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/adapter/classAdapter.png"));
		classAdapterButton.setGraphic(new ImageView(classAdapterImage));
		Image objectAdapterImage = new Image(
				getClass().getResourceAsStream("/cn/edu/scau/cmi/wangmaoren/adapter/objectAdapter.png"));
		objectAdapterButton.setGraphic(new ImageView(objectAdapterImage));

		classAdapterButton.setOnAction((e) -> {
			classAdapterAddStudent();
		});
		objectAdapterButton.setOnAction((e) -> {
			objectAdapterAddStudent();
		});

	}
}

/*
 * public class AdapterClient {
 * 
 * public static void main(String[] args) { List<Student> studentList = new
 * ArrayList<Student>(); Target classTarget=new AdapterOfClass(); Target
 * objectTarget = new AdapterOfObject();
 * 
 * studentList.add(new Student(10, "a")); studentList.add(new Student(1, "b"));
 * studentList.add(new Student(8, "c")); studentList.add(new Student(5, "d"));
 * 
 * studentList = classTarget.doTarget(studentList);
 * 
 * for (int i = 0; i < studentList.size(); i++) { Student s =
 * studentList.get(i); System.out.println(s.getId() + s.getName()); }
 * 
 * studentList.add(new Student(89, "e")); studentList.add(new Student(90, "f"));
 * studentList.add(new Student(66, "g"));
 * 
 * studentList = objectTarget.doTarget(studentList);
 * 
 * for (int i = 0; i < studentList.size(); i++) { Student s =
 * studentList.get(i); System.out.println(s.getId() + s.getName()); }
 * 
 * } }
 */